package com.fs.user.controller;

import com.fs.pandaFund.common.util.ResponseData;
import com.fs.pandaFund.common.util.ResponseDataUtil;
import com.fs.user.service.impl.UserServiceImpl;
import com.fs.user.vo.LoginBean;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * Author:HOU
 * date:2023/2/14  10:15
 * 案例:
 **/
@RestController
@RequestMapping("user")
public class LoginController {
    @Autowired
    private Producer producer;
    @Autowired
    private UserServiceImpl userServiceImpl;
    @GetMapping("captcha.jpg")
    public void captcha(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");

        // 生成文字验证码
        String text = producer.createText();
        // 生成图片验证码
        BufferedImage image = producer.createImage(text);
        // 保存到验证码到 session
        request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, text);

        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);
        IOUtils.closeQuietly(out);
    }
//    @PostMapping("login")
//    public ResponseData login(@RequestBody LoginBean loginBean) throws Exception{
//       userServiceImpl.loginByPassword(loginBean);
//
//        return ResponseDataUtil.buildOk();
//
//    }
}
